﻿using System.Windows.Forms;
using System.Xml.Linq;
using TomanuExtensions;

namespace Raytracer.Configurations
{
    public class FormState
    {
        private FormWindowState m_window_state = FormWindowState.Normal;
        private int m_left = 100;
        private int m_top = 100;
        private int m_width = 800;
        private int m_height = 600;
        private int m_log_splitter_pos = 300;
        private int m_spliter1_pos = 300;
        private int m_spliter2_pos = 300;
        private PropertySort m_property_sort = PropertySort.Categorized;

        internal FormState()
        {
        }

        internal FormState(XElement a_node)
        {
            m_window_state = EnumExtensions.Parse<FormWindowState>(
                a_node.Element("WindowState").Value);
            m_left = a_node.Element("Left").Value.ToInt();
            m_top = a_node.Element("Top").Value.ToInt();
            m_width = a_node.Element("Width").Value.ToInt();
            m_height = a_node.Element("Height").Value.ToInt();
            m_log_splitter_pos = a_node.Element("LogSplitterPos").Value.ToInt();
            m_spliter1_pos = a_node.Element("Splitter1Pos").Value.ToInt();
            m_spliter2_pos = a_node.Element("Splitter2Pos").Value.ToInt();
            m_property_sort = EnumExtensions.Parse<PropertySort>(
                a_node.Element("PropertySort").Value);
        }

        internal XElement GetAsXml()
        {
            return new XElement("FormState",
                new XElement("WindowState", m_window_state),
                new XElement("Left", m_left),
                new XElement("Top", m_top),
                new XElement("Width", m_width),
                new XElement("Height", m_height),
                new XElement("LogSplitterPos", m_log_splitter_pos),
                new XElement("Splitter1Pos", m_spliter1_pos),
                new XElement("Splitter2Pos", m_spliter2_pos),
                new XElement("PropertySort", m_property_sort));
        }

        public void RestoreFormState(Form a_form)
        {
            a_form.WindowState = m_window_state;
            a_form.Left = m_left;
            a_form.Top = m_top;
            a_form.Width = m_width;
            a_form.Height = m_height;
        }

        public void SaveFormState(Form a_form)
        {
            m_window_state = a_form.WindowState;

            if (a_form.WindowState == FormWindowState.Normal)
            {
                m_left = a_form.Left;
                m_top = a_form.Top;
                m_width = a_form.Width;
                m_height = a_form.Height;
            }

            Configuration.Instance.Save();
        }

        public int LogSplitterPos
        {
            get
            {
                return m_log_splitter_pos;
            }
            set
            {
                m_log_splitter_pos = value;

                Configuration.Instance.Save();
            }
        }

        public int Spliter1Pos
        {
            get
            {
                return m_spliter1_pos;
            }
            set
            {
                m_spliter1_pos = value;

                Configuration.Instance.Save();
            }
        }

        public int Spliter2Pos
        {
            get
            {
                return m_spliter2_pos;
            }
            set
            {
                m_spliter2_pos = value;

                Configuration.Instance.Save();
            }
        }

        public PropertySort PropertySort
        {
            get
            {
                return m_property_sort;
            }
            set
            {
                m_property_sort = value;

                Configuration.Instance.Save();
            }
        }
    }
}